package com.hdu.ordered;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        int cpuCoreSize = Runtime.getRuntime().availableProcessors();
        OrderedThreadPool orderedThreadPool = new OrderedThreadPool(
                cpuCoreSize, 2 * cpuCoreSize,
                1L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(100),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy(),
                new HashSelector()
        );
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                TestKeyRunnable task = new TestKeyRunnable("KEY1", i + 1 + "");
                orderedThreadPool.execute(task);
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                TestKeyRunnable task = new TestKeyRunnable("KEY2", i + 1 + "");
                orderedThreadPool.execute(task);
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                TestKeyRunnable task = new TestKeyRunnable("KEY3", i + 1 + "");
                orderedThreadPool.execute(task);
            }
        }).start();

        //key:KEY3 id:1 thread:pool-1-thread-1 run
        //key:KEY2 id:1 thread:pool-1-thread-2 run
        //key:KEY1 id:1 thread:pool-1-thread-3 run
        //key:KEY2 id:2 thread:pool-1-thread-2 run
        //key:KEY3 id:2 thread:pool-1-thread-1 run
        //key:KEY2 id:3 thread:pool-1-thread-2 run
        //key:KEY1 id:2 thread:pool-1-thread-3 run
        //key:KEY1 id:3 thread:pool-1-thread-3 run
        //key:KEY1 id:4 thread:pool-1-thread-3 run
        //key:KEY1 id:5 thread:pool-1-thread-3 run
        //key:KEY1 id:6 thread:pool-1-thread-3 run
        //key:KEY1 id:7 thread:pool-1-thread-3 run
        //key:KEY2 id:4 thread:pool-1-thread-2 run
        //key:KEY3 id:3 thread:pool-1-thread-1 run
        //key:KEY2 id:5 thread:pool-1-thread-2 run
        //key:KEY1 id:8 thread:pool-1-thread-3 run
        //key:KEY2 id:6 thread:pool-1-thread-2 run
        //key:KEY3 id:4 thread:pool-1-thread-1 run
        //key:KEY2 id:7 thread:pool-1-thread-2 run
        //key:KEY1 id:9 thread:pool-1-thread-3 run
        //key:KEY2 id:8 thread:pool-1-thread-2 run
        //key:KEY3 id:5 thread:pool-1-thread-1 run
        //key:KEY2 id:9 thread:pool-1-thread-2 run
        //key:KEY1 id:10 thread:pool-1-thread-3 run
        //key:KEY2 id:10 thread:pool-1-thread-2 run
        //key:KEY3 id:6 thread:pool-1-thread-1 run
        //key:KEY3 id:7 thread:pool-1-thread-1 run
        //key:KEY3 id:8 thread:pool-1-thread-1 run
        //key:KEY3 id:9 thread:pool-1-thread-1 run
        //key:KEY3 id:10 thread:pool-1-thread-1 run
    }
}
